package bjut.zrpc.core.optimization.processor;



import bjut.zrpc.core.optimization.message.ZRpcResponseMessage;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: zhuzhiming
 * @Date: 2022/6/24 16:11
 */
public class UnprocessedRequest {

    private static final ConcurrentHashMap<String, CompletableFuture<ZRpcResponseMessage<Object>>> UNPROCESSED_MAP = new ConcurrentHashMap<>();

    public void putUnprocessedRequest(String requestId, CompletableFuture<ZRpcResponseMessage<Object>> completableFuture) {
        UNPROCESSED_MAP.put(requestId, completableFuture);
    }

    public void  complete(ZRpcResponseMessage<Object> responseMessage) {

        CompletableFuture<ZRpcResponseMessage<Object>> completableFuture = UNPROCESSED_MAP.get(responseMessage.getRequestId());
        if (completableFuture != null) {
            completableFuture.complete(responseMessage);
        } else {
            throw new RuntimeException();
        }

    }


}
